package com.lanlengran.hotupdatetest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;

import com.lanlengran.hotupdatetest.hook.DexUtils;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        verifyStoragePermissions(MainActivity.this);
        DexUtils.init(this);

        String appName = getResources().getString(R.string.app_name);
        try {
            InputStream is = getAssets().open("ic_launcher.png");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void click(View view) {
//        PathClassLoader pathClassLoader=new PathClassLoader("/sdcard/output.dex",null);
//        try{
//            Class<?> clazz = pathClassLoader.loadClass("com.lanlengran.hotupdatetest.BugClass");
//            Constructor<?> cons=clazz.getConstructor(Context.class);
//            cons.newInstance(MainActivity.this);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
            if (view.getId()==R.id.btn_to_plugin_activity){
                startActivity(new Intent(MainActivity.this,PluginActivity.class));
                return;
            }

        try {
            Class<?> clazz = Class.forName("com.lanlengran.hotupdatetest.BugClass");
            Constructor<?> cons = clazz.getConstructor(Context.class);
            cons.newInstance(MainActivity.this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void verifyStoragePermissions(Activity activity) {
        try {
            //检测是否有写的权限
            int permission = ActivityCompat.checkSelfPermission(activity,
                    "android.permission.READ_EXTERNAL_STORAGE");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                String[] permissionList={"android.permission.READ_EXTERNAL_STORAGE"};
                // 没有写的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(activity,permissionList ,1);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}